<template>
  <div class="content">
    <el-button @click="openFormDialog(form,'add')">新增权限</el-button>
    <div style="display: flex;flex-direction: column;align-items: flex-end;">

      <el-table
          :data="permissionList"
          row-key="id"
          stripe
          :row-style="{height:'40px'}"
          :cell-style="{padding:'0px'}"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            prop="name"
            label="权限名称">
        </el-table-column>
        <el-table-column
            prop="code"
            label="权限名称">
        </el-table-column>
        <el-table-column
            prop="introduce"
            label="权限介绍">
        </el-table-column>
        <el-table-column
            label="操作"
            width="200">
          <template slot-scope="scope">
            <el-button @click="openFormDialog(scope.row,'edit')" type="text" size="small">编辑</el-button>
            <el-button @click="deleteById(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination style="margin-top: 5px"
                     @size-change="currentSizeChange"
                     @current-change="currentChange"
                     :current-page="currentPage"
                     :page-sizes="[10, 20, 30, 40]"
                     :page-size="limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>

      <el-dialog title="修改权限" :visible.sync="dialogFormVisible" width="600px">
        <el-form :model="form" ref="form">
          <el-form-item label="权限名" prop="name" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input v-model="form.name" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="权限码" prop="code" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input v-model="form.code" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="权限介绍" prop="introduce" :label-width="formLabelWidth"
                        :rules="[
                                      { required: true, message: '请填写完整'}
                                    ]">
            <el-input v-model="form.introduce" autocomplete="off"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-dialog>

    </div>

  </div>
</template>

<script>
export default {
  name: "PermissionList",
  components: {

  },
  data(){
    return{
      page: 1,
      limit: 10,
      total: 0,
      currentPage:1,
      ofType:'',
      permissionList: [],
      form:{
        id:'',
        name:'',
        code:'',
        introduce:'',
      },
      dialogFormVisible:false,
      formLabelWidth:'120px'
    }
  },
  created(){
    this.getPermissions()
  },
  methods:{
    //获取权限列表
    getPermissions(){
      let url = 'permission/'+this.page+"/"+this.limit;
      this.$http.get(url).then(res => {
        this.total = res.data.data.data.total
        this.permissionList = res.data.data.data.records
      })
    },
    //页面发生改变
    currentChange(value){
      this.page = value
      this.getPermissions()
    },
    currentSizeChange(value){
      this.limit = value
      this.getPermissions()
    },
    //提交表单
    submitForm(formName){
      if(this.form.id){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "permission"
            this.$http.put(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getPermissions()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }else{
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "permission"
            this.$http.post(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getPermissions()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }
    },
    //根据ID删除
    deleteById(row) {
      this.$confirm('确定要删除这条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'permission/'+row.id
        this.$http.delete(url).then(res=>{
          if(res.data.code===0){
            this.getPermissions()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //打开编辑框
    openFormDialog(row,type){
      this.ofType = type
      this.dialogFormVisible = true;
      //添加菜单
      if(type==='add'){
        this.form = {}
      }
      //编辑菜单
      if(type==='edit'){
        if(!row.parentId){
          this.ofType = 'add'
        }
        this.form = {...row}
      }
    },
    //清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
